//
//  AkuWeatherModel.m
//  AkuCareLink
//
//  Created by 阿酷 on 16/7/5.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

#import "AkuWeatherModel.h"
#import "AkuNetWorkTool.h"
#import "AkuNetWorkDefine.h"



@implementation AkuWeatherModel

+ (void)weatherWithCurrentCity:(NSString *)cityName finishBlock:(void (^)(AkuWeatherModel *weatherModel))finishBlock errorBlock:(void (^)())errorBlock {
    
    /*
     请求地址：http://api.map.baidu.com/telematics/v3/weather
     请求参数：（city填什么，大家都懂的！）{@"location":city,@"output":@"json",@"ak":@"17IvzuqmKrK1cGwNL6VQebF9"};
     */
    // @"http://api.map.baidu.com/telematics/v3/weather?location=&#x4E0A;&#x6D77;&output=json&ak=17IvzuqmKrK1cGwNL6VQebF9"
    NSString *str = AkuWeather_URL(cityName);

    // 指定url字符串的某个部分进行转换  中文 转义
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSLog(@"%@", str);
    
    NSString *urlStr = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary * dic = @{@"location":cityName,@"output":@"json",@"ak":@"17IvzuqmKrK1cGwNL6VQebF9"};
    
    [[AkuNetWorkTool shareManager] GET:urlStr parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"results"];
        
        // 如果 请求参数 提交了两个 或两个以上 城市 则返回对应个数的字典 存入数组里
        NSDictionary *cityDict = [arr firstObject];
        
        AkuWeatherModel *model = [[AkuWeatherModel alloc] init];
        
        // 先取出 城市 信息
        model.currentCity = cityDict[@"currentCity"];
        
        NSArray *weatherData = cityDict[@"weather_data"];
        
        // 数组 里有 最近几天的 天气预报 但我只需要第一条 所以 只需要取 第一条
        NSDictionary *weatherDict = [weatherData firstObject];
        
        [model setValuesForKeysWithDictionary:weatherDict];
        
        // 回调 返回 天气 model
        finishBlock(model);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
     
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

@end

 












